import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @作者 ： pwx5328113 @时间 ： 2023/2/27 @测试点 : rename table 异常场景执行
 **/

public class Compatibility_Grammar_Renametable_Case0004 {
	public static void main(String[] args) throws Exception {
		if (args.length < 4) {
			System.out.print("need para: host:ip,db_b,user,password");
			return;
		}
		String url = "jdbc:postgresql://" + args[0] + "/" + args[1] + "?LoggerLevel=off";
		Connection conn_b = DriverManager.getConnection(url, args[2], args[3]);
		Statement stmt_b = null;
		stmt_b = conn_b.createStatement();
		String err1_msg = null;
		String err2_msg = null;
		try {
			int rename_b = stmt_b.executeUpdate("create table test1(id int);rename table test1 to test1");
			System.out.println("rename_b_result = " + rename_b);
		} catch (SQLException e) {
			err1_msg = e.getMessage();
			System.out.println("err_msg = " + err1_msg);
			System.out.println("rename_b_result = " + stmt_b.executeUpdate("drop table if exists test1;"));
		}
		try {
			int rename_b = stmt_b
					.executeUpdate("create table test1(id int);create table test2(id int);rename table test1 to test2");
			System.out.println("rename_b_result = " + rename_b);
		} catch (SQLException e) {
			err2_msg = e.getMessage();
			System.out.println("err_msg = " + err2_msg);
			System.out.println("rename_b_result = " + stmt_b.executeUpdate("drop table if exists test1;"));
		}
		stmt_b.close();
		conn_b.close();
		String err1_flag = " ERROR: relation \"public.test1\" already exists";
		String err2_flag = " ERROR: relation \"public.test2\" already exists";
		if (err1_msg.indexOf(err1_flag) != -1) {
			System.out.println("rename table as a same tablename returns error successfully");
		}
		if (err2_msg.indexOf(err2_flag) != -1) {
			System.out.println("rename table as a exsits tablename returns error successfully");
		}
	}
}
